package com.huwaiwai.disruptor.demo03;

import com.huwaiwai.productor_consumer.Producer;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.nio.ByteBuffer;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * 测试
 * Created by huweiwei on 2017/5/15.
 */
public class Domain {

    public static void main(String [] args) throws InterruptedException {

        Executor executor = Executors.newCachedThreadPool();
        PCDataFactory factory = new PCDataFactory();
        int bufferSize = 1024;
        Disruptor<PCData> disruptor = new Disruptor<>(
                factory,
                bufferSize,
                executor,
                ProducerType.MULTI,
                new BlockingWaitStrategy());

        disruptor.handleEventsWithWorkerPool(
                new Demo03Consumer(),
                new Demo03Consumer(),
                new Demo03Consumer(),
                new Demo03Consumer(),
                new Demo03Consumer());

        disruptor.start();

        RingBuffer<PCData> ringBuffer = disruptor.getRingBuffer();
        Demo03Producer producer = new Demo03Producer(ringBuffer);
        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long i = 0; true; i++) {
            bb.putLong(0, i);
            producer.push(bb);
            Thread.sleep(100);
            System.out.println("add data " + i);
        }
    }
}
